var PaymentTable = {
    id: "PaymentTable",
    seItem: null,
    table: null,
    layerIndex: -1
};

function searchTotal() {
    $.ajax({
        type: "GET",
        url: Feng.ctxPath + '/paymentTotal/sumBorrowAmount',
        data: PaymentTable.formParams(),
        dataType: "json",
        success: function (data) {
            $('#sumMoney').html("(" + data.borrowAmount_sum + ")元");
        }
    });
    var reach = $("#doctorTcInputPublics", parent.document).val();
    var abbreviation = $("#abbreviation", parent.document).val();
    var startTime = $('#startTime').val();
    var endTime = $('#endTime').val();
    var fieldTc = $('#fieldTc').val();
    var customerStatus = $('#customerStatus').val();
    $.ajax({
        type: "post",
        url: Feng.ctxPath + "/paymentTotal/getPaymentTotalReport",
        dataType: "json",
        data: {
            'reach': reach,
            'startTime': startTime,
            'endTime': endTime,
            'fieldTc': fieldTc,
            'customerStatus': customerStatus,
            'abbreviation':abbreviation
        },
        success: function (data) {
            var arr = [];
            arr.push({radio: true});
            arr.push({"title": '客户姓名', "field": '客户姓名', switchable: true, sortable: true, align: 'center', width: 110});
            arr.push({"title": '电话', "field": '电话', switchable: true, sortable: true, align: 'center', width: 140});
            arr.push({
                "title": '性别', "field": '性别', switchable: true, sortable: true, align: 'center', width: 90,
                formatter: function (value, row, index) {
                    if (value == 1) {
                        return "男"
                    } else if (value == 2) {
                        return "女"
                    } else if (value == 3) {
                        return "未知"
                    }
                }
            });
            arr.push({"title": '年龄', "field": '年龄', switchable: true, sortable: true, align: 'center', width: 90});
            arr.push({"title": '时间', "field": '时间', switchable: true, sortable: true, align: 'center', width: 170});
            arr.push({"title": '现场tc', "field": 'tc', switchable: true, sortable: true, align: 'center', width: 100});
            arr.push({"title": '收费人', "field": '收费人', switchable: true, sortable: true, align: 'center', width: 100});
            arr.push({"title": '客户状态', "field": '客户状态', switchable: true, sortable: true, align: 'center', width: 110});
            if (data[0] == null || data[0] == undefined || data[0] == "") {
                console.log('返回数据为空');
            } else {
                for (var i = 0; i < (Object.keys(data[0])).length; i++) {
                    var property = (Object.keys(data[0]))[i];
                    if (property != 'customerId' && property != '客户姓名' && property != '电话' && property != '性别' && property != '年龄' && property != '时间' && property != 'tc' && property != '收费人' && property != '客户状态' && property != 'tc' && property != '备注') {
                        arr.push({
                            "title": property,
                            "field": property,
                            switchable: true,
                            sortable: true,
                            align: 'center',
                            width: 110
                        });
                    }
                }
            }
            arr.push({
                "title": '备注', "field": '备注', switchable: true, sortable: true, align: 'center', width: 120,
                formatter: function (value, row, index) {
                    return '<span title="' + value + '">' + value + '</span>';
                },
                cellStyle: function (value, row, index) {
                    return {css: {"overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
                }
            });
            $('#PaymentTable').bootstrapTable('destroy').bootstrapTable({
                data: data,
                toolbar: '#toolbar',
                cache: false,
                striped: true,
                sidePagination: "client",
                sortOrder: "desc",
                pageSize: 500,
                pageNumber: 1,
                pageList: [500, 1000],
                showToggle: true,
                showColumns: true,
                showExport: true,
                exportDataType: "basic",
                pagination: true,
                strictSearch: true,
                search: true,
                columns: arr,
                height: $(window).height() - 76
            });
            phonePrivileges();
        }
    });
};

$("#doctorTcSearchAlls", parent.document).click(function () {//主页搜索
    searchTotal();
});

var parseParam = function (param) {
    var paramStr = "";
    $.each(param, function (i) {
        paramStr += '&' + getSingleData(param, i)
    });
    return paramStr.substr(1)
};
var getSingleData = function (param, key) {
    return key + "=" + param[key]
};

//导出
PaymentTable.paymentTableExport = function () {
    var param = parseParam(PaymentTable.formParams());
    location.href = Feng.ctxPath + '/paymentTotal/paymentTableExport?' + param;
};


PaymentTable.resetSearch = function () {
    $("#doctorTcInputPublics", parent.document).val('');
    $('#fieldTc').val('');
    $('#customerStatus').val('');
    $("#startTime").val("");
    $("#endTime").val("");
    searchTotal();
};

PaymentTable.formParams = function () {
    var queryData = {};
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['fieldTc'] = $('#fieldTc').val();
    queryData['reach'] = $("#doctorTcInputPublics", parent.document).val();
    queryData['abbreviation'] = $("#abbreviation", parent.document).val();
    queryData['customerStatus'] = $('#customerStatus').val();
    return queryData;
};

PaymentTable.search = function () {
    searchTotal();
};

var AccountsTime = $("#sttleAccountsTime").val();
PaymentTable.today = function () {//今日
    if ($("#naturalDaySearch").val() == "1") {
        $("#startTime").val(setDateTime(new Date(),0) + " " + "00:00:00");
        $("#endTime").val(setDateTime(new Date(),0) + " " + "23:59:59");
    } else {
        $("#startTime").val(setDateTime(new Date(),1) + " " + AccountsTime);
        $("#endTime").val(setDateTime(new Date(),0) + " " + AccountsTime)
    }
    $("#timeValue").val('1');
    searchTotal();
};

PaymentTable.yesterday = function () {//昨日
    if ($("#naturalDaySearch").val() == "1") {
        $("#startTime").val(setDateTime(new Date(),1) + " " + "00:00:00");
        $("#endTime").val(setDateTime(new Date(),1) + " " + "23:59:59")
    } else {
        $("#startTime").val(setDateTime(new Date(),2) + " " + AccountsTime);
        $("#endTime").val(setDateTime(new Date(),1) + " " + AccountsTime)
    }
    $("#timeValue").val('2');
    searchTotal()
};

PaymentTable.thisWeek = function () {//本周
    var date = new Date();
    var currentWeek = date.getDay();
    var stepSunDay = -currentWeek + 1;
    if (currentWeek == 0) {stepSunDay = -7;}
    var stepMonday = 7 - currentWeek;
    var time = date.getTime();
    var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
    var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
    if ($("#naturalDaySearch").val() == "1") {
        $("#startTime").val(setDateTime(monday,0) + " " + "00:00:00");
        $("#endTime").val(setDateTime(sunday,0) + " " + "23:59:59");
    } else {
        $("#startTime").val(setDateTime(monday,1) + " " + AccountsTime);
        $("#endTime").val(setDateTime(sunday,0) + " " + AccountsTime)
    }
    $("#timeValue").val('3');
    searchTotal();
};

PaymentTable.thisMonth = function () {//本月
    if ($("#naturalDaySearch").val() == "1") {
        $("#startTime").val(firstMonthTime(new Date(),0) + " " + "00:00:00");
        $("#endTime").val(setMonthTime(new Date(),0) + " " + "23:59:59");
    } else {
        $("#startTime").val(setMonthTime(new Date(),1) + " " + AccountsTime);
        $("#endTime").val(setMonthTime(new Date(),0) + " " + AccountsTime);
    }
    $("#timeValue").val('4');
    searchTotal();

};

PaymentTable.naturalDaySearch = function () {//自然时间
    $("#naturalDaySearch").val('1');
    if ($("#timeValue").val() == "1") {
        $("#startTime").val(setDateTime(new Date(),0) + " " + "00:00:00");
        $("#endTime").val(setDateTime(new Date(),0) + " " + "23:59:59");
    } else if ($("#timeValue").val() == "2") {
        $("#startTime").val(setDateTime(new Date(),1) + " " + "00:00:00");
        $("#endTime").val(setDateTime(new Date(),1) + " " + "23:59:59");
    } else if ($("#timeValue").val() == "3") {
        var date = new Date();
        var currentWeek = date.getDay();
        var stepSunDay = -currentWeek + 1;
        if (currentWeek == 0) {stepSunDay = -7;}
        var stepMonday = 7 - currentWeek;
        var time = date.getTime();
        var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
        var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
        $("#startTime").val(setDateTime(monday,0) + " " + "00:00:00");
        $("#endTime").val(setDateTime(sunday,0) + " " + "23:59:59");
    }else if ($("#timeValue").val() == "4") {
        $("#startTime").val(firstMonthTime(new Date(),0) + " " + "00:00:00");
        $("#endTime").val(setMonthTime(new Date(),0) + " " + "23:59:59");
    }
    $(".timeBox > div button").eq(0).addClass('actionClick').siblings().removeClass('actionClick');
    searchTotal();
};

PaymentTable.AccountsSearch = function () {//扎帐时间
    $("#naturalDaySearch").val('2');
    if ($("#timeValue").val() == "1") {
        $("#startTime").val(setDateTime(new Date(),1) + " " + AccountsTime);
        $("#endTime").val(setDateTime(new Date(),0) + " " + AccountsTime);
    } else if ($("#timeValue").val() == "2") {
        $("#startTime").val(setDateTime(new Date(),2) + " " + AccountsTime);
        $("#endTime").val(setDateTime(new Date(),1) + " " + AccountsTime);
    } else if ($("#timeValue").val() == "3") {
        var date = new Date();
        var currentWeek = date.getDay();
        var stepSunDay = -currentWeek + 1;
        if (currentWeek == 0) {stepSunDay = -7;}
        var stepMonday = 7 - currentWeek;
        var time = date.getTime();
        var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
        var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
        $("#startTime").val(setDateTime(monday,1) + " " + AccountsTime);
        $("#endTime").val(setDateTime(sunday,0) + " " + AccountsTime)
    }else if ($("#timeValue").val() == "4") {
        $("#startTime").val(setMonthTime(new Date(),1) + " " + AccountsTime);
        $("#endTime").val(setMonthTime(new Date(),0) + " " + AccountsTime);
    }
    $(".timeBox > div button").eq(1).addClass('actionClick').siblings().removeClass('actionClick');
    searchTotal();
};

function phonePrivileges(){   //隐藏手机号
    var phonePrivileges=parent.$("#phonePrivileges").val();
    if(phonePrivileges == 2){
        $('#PaymentTable').bootstrapTable('hideColumn', '电话');
    }
}
$(function () {
    PaymentTable.today();
    checkDate = function () {searchTotal();};
    $(document).on('click', '#laydate_clear,#laydate_today,#laydate_ok', function () {searchTotal();});
});